function resizeKanban(){
	var bodyHeight = $(window).height();

	if(bodyHeight < 300){
		$("#group_users ul").height(bodyHeight - 310);
		$("ul.groups, .list_items").height(300);
	}else{
		$("#group_users ul").height(bodyHeight - 131);
		$("ul.groups, .list_items").height(bodyHeight - 121);
	}
}

$(window).resize(function() {
	resizeKanban();
});

$(document).ready(function() {	
	loadGroups();
	loadUsers();
	resizeKanban();

	$("#menu_filter").click(function(){
		if($("#toolsbar .filter").css("display") == "none"){
			$("#toolsbar").slideUp('fast',function(){
				$(".tools_left > div",this).hide();
				$(".filter",this).show();

				$('#findUsers').keyup(function(){
					var valField = new RegExp($(this).val(),"i");
					$("#users ul li").each(function(){
						if($(".title_bar span",this).text().search(valField) >= 0)
							$(this).css("display","block");
						else
							$(this).css("display","none");
					});
				});

				$(this).slideDown('slow');
			});
		}
	});

	$("#menu_add").click(function(){
		if($("#toolsbar .add").css("display") == "none"){
			$("#toolsbar").slideUp('fast',function(){
				$(".tools_left > div",this).hide();
				$(".add",this).show();

				$(this).slideDown('slow');
			});
		}
	});

	$("#toolsbar .cancel").click(function(){
		$("#toolsbar").slideUp('fast',function(){
			$(".tools_left > div",this).hide();
			$(".title",this).show();

			$(this).slideDown('slow');
		});
	});

	$("#newUser").click(function(){		
		$.ajax({
			url: SITE+"app/Views/V_User.php?f=newUser",
			success: function(data){
				var obj = JSON.decode(data);
				loadHTMLUser(obj);
			}
		});
	});	
});

function showModal(){
	resizeKanban();
}

function closeModal(){
	resizeKanban();
}


/******************************************************************************************************************
Grupos Functions
******************************************************************************************************************/
function loadGroups(){
	$.ajax({
		url: SITE+"app/Views/V_UserGroup.php?f=getGroups",
		async: false,
		success: function(data){
			var obj = JSON.decode(data);
			$("#groups").html('<div class="list_items right"><a title="Fechar" class="close_box"></a><div id="group_users"></div></div>'+obj[1]);

			$(".groups li").droppable({
				hoverClass: "active_drop",
				drop: function( event, ui ) {
					var obj		= $(ui.draggable).children();			
					var	idGroup	= $(this).attr("id").replace("group_","");					
					
					if($(obj).attr("id").search("userGroup_") == 0){
						var ids	= obj.attr("id").replace("userGroup_","");
						ids = ids.split("-");

						if(ids[1] != idGroup)
							changeUserGroup(ids[0],ids[1],idGroup);										
					}else{
						saveUserGroup(obj,idGroup);
					}
				}
			});			

			$(".groups li .description").click(function(){
				setGroup($(this).parent());
			});
		}
	});

	return false;
}

function setGroup(obj){
	var	id = $(obj).attr("id").replace("group_","");

	if($("ul.groups").data("group") != id){
		loadUsersGroup(id);
	}else{
		openBoxGroup(id);
	}
	
	$("ul.groups").data("group",id);
}

function openBoxGroup(idGroup){
	var objListItems = $(".list_items.right");

	$(".groups li").removeClass("selected");
	$("#group_"+idGroup).addClass("selected");

	if(objListItems.css("display") == "none"){
		objListItems.show("slide",{direction:"right"},function(){
			$(".list_items.right .close_box").show();

			var boxSprints = $(".groups").width();
			$(".groups").width(boxSprints-268);
		});
	}else{
		$(".close_box",objListItems).show();
	}
}


function updateGroups(){
	$.ajax({
		url: SITE+"app/Views/V_UserGroup.php?f=getGroupsSimple",
		async: false,
		success: function(data){
			var obj = JSON.decode(data);
			if(obj[0]){
				var x;
				for(x in obj[1]){				
					$("#group_"+obj[1][x].idUserGroup+" .count_users").html(obj[1][x].users);
				}
			}
		}
	});
}

/******************************************************************************************************************
Usuários Functions
******************************************************************************************************************/
function loadUsers(){
	$.ajax({
		url: SITE+"app/Views/V_User.php?f=getUsersWithoutGroup",
		success: function(data){
			var obj = JSON.decode(data);
			$("#users").html(obj[1]);

			$("#users ul li").draggable({
				helper: "clone",
				zIndex: 9999,
				scroll:false,
				appendTo: "body",
				connectToSortable: "#group_users ul",
				start: function(event, ui) {					
					$(".details",$(ui.helper)).css("display","none");
					$(".buttons",$(ui.helper)).css("display","none");

					$(".details",$(this)).css("display","none");
					$(".buttons",$(this)).css("display","none");
				}
			});

			$("#users ul li .title_bar").click(function(){
				$(".buttons",$(this).parent()).slideToggle("fast");
			});

			$("#users ul li .title_bar").dblclick(function(){
				var id = $(this).parent().attr("id").replace("user_","");
				editUser(id);								
			});

			$("#users ul li button.expand").click(function(){
				var obj = $(this);
				$(".details",$(this).parent().parent()).slideToggle("fast",function(){
					if($(this).css("display") == "none"){
						obj.html("+");
					}else{
						obj.html("-");
					}
				});
			});
		}
	});
}

function loadHTMLUser(obj){
	var html = "";

	html = obj[1];
	openBox("overlay", html);

	if(obj[0]){
		$("#formUser").validate({
			submitHandler: function(form) {
				var query = $(form).serialize();
				
				$.ajax({
					url: SITE+"app/Views/V_User.php?f=saveUser&"+query,
					async: false,
					success: function(data){
						var obj = JSON.decode(data);
						if(obj[0]){			
							loadUsers();
						}

						resultMensage(obj, obj[0]);
					}
				});

				return false;
			}
		});

		$("#formUser #status").change(function(){
			if($("#formUser :input[name='id']").val() > 0){
				if($(this).val() == 1){
					$("#formUser #labelDateEnd").addClass("hide");
					$("#formUser #dateEnd").attr("disabled","disabled");
				}else{
					$("#formUser #labelDateEnd").removeClass("hide");
					$("#formUser #dateEnd").attr("disabled","");
				}
			}
		});

		if($("#formUser #status").val()){
			$("#formUser #dateEnd").attr("disabled","disabled");
		}
		
		$(".date-pick").dateinput();
	}else{
		eval(obj[2]);
	}
}

function editUser(id){
	$.ajax({
		url: SITE+"app/Views/V_User.php?f=editUser&id="+id+"&all=true",
		success: function(data){
			var obj = JSON.decode(data);
			loadHTMLUser(obj);
		}
	});
}

function deleteUser(id){
	openBox("modalDelete");

	$("#modalDelete #confirm").click(function(){
		$.ajax({
			url: SITE+"app/Views/V_User.php?f=deleteUser&id="+id,
			success: function(data){
				var obj = JSON.decode(data);
				if(obj[0]){
					$("#user_"+id).remove();
				}
				
				resultMensage(obj, true);
			}
		});
	});
}
/******************************************************************************************************************
Equipes/Usuários Functions
******************************************************************************************************************/
function loadUsersGroup(idGroup){
	$.ajax({
		url: SITE+"app/Views/V_UserGroup.php?f=getUsersGroup&idGroup="+idGroup,
		async: false,
		success: function(data){
			var obj = JSON.decode(data);
			$("#group_users").html(obj[1]);

			$("#group_users > ul").height($(".list_items.right").height()-10);

			$("#group_users > ul").sortable({			
				placeholder: 'placeholder',
				start: function(event, ui) {
					$(".details",$(ui.item)).css("display","none");
					$(".buttons",$(ui.item)).css("display","none");
				},
				beforeStop: function(event, ui) {
					var idUser = $(ui.item).children().attr("id").replace("user_","");					

					if(idUser > 0)
						ui.item.remove(); 
				}
			});

			$("#group_users button.expand").click(function(){
				var obj = $(this);
				$(".details",$(this).parent().parent()).slideToggle("fast",function(){
					if($(this).css("display") == "none"){
						obj.html("+");
					}else{
						obj.html("-");
					}
				});
			});
			
			$(".list_items.right .close_box").unbind("click");
			$(".list_items.right .close_box").click(function(){				
				$(this).hide();
				$(".list_items.right").hide("slide",{direction:"right"});
				$(".groups").width("auto");
			});

			$("#group_users .title_bar").click(function(){
				$("> .buttons",$(this).parent()).slideToggle("fast");
			});

			$("#group_users .title_bar").dblclick(function(){
				var ids = $(this).parent().attr("id").replace("userGroup_","");
				ids = ids.split("-");
				editUser(ids[0]);
			});
			
			openBoxGroup(idGroup);
		}
	});
}

function saveUserGroup(obj, idGroup){
	var idUser = $(obj).attr("id").replace("user_","");

	if(idUser > 0 && idGroup > 0){
		setGroup($("#group_"+idGroup));

		$.ajax({
			url: SITE+"app/Views/V_UserGroup.php?f=saveUserGroup&idUser="+idUser+"&idGroup="+idGroup,
			async: false,
			success: function(data){
				var obj = JSON.decode(data);
				if(obj[0]){				
					loadUsersGroup(idGroup);
					updateGroups();
					loadUsers();
				}

				resultMensage(obj, true);
			}
		});
	}
}

function deleteUserGroup(idUser, idGroup){
	openBox("modalDelete");

	$("#modalDelete #confirm").click(function(){
		$.ajax({
			url: SITE+"app/Views/V_UserGroup.php?f=deleteUserGroup&idUser="+idUser+"&idGroup="+idGroup,
			success: function(data){
				var obj = JSON.decode(data);
				if(obj[0]){
					$("#userGroup_"+idUser+"-"+idGroup).remove();
					updateGroups();
					loadUsers();
				}
				resultMensage(obj, true);
			}
		});
	});
}

function changeUserGroup(idUser,idGroup,idNewGroup){
	$.ajax({
		url: SITE+"app/Views/V_UserGroup.php?f=changeUserGroup&idUser="+idUser+"&idGroup="+idGroup+"&idNewGroup="+idNewGroup,
		success: function(data){
			var obj = JSON.decode(data);
			if(obj[0]){
				$("#userGroup_"+idUser+"-"+idGroup).remove();
				updateGroups();
			}
			resultMensage(obj, true);
		}
	});
}